import { httpGet } from '../utils/request.js';

let timer; //定时器，勿动
let model = 'MJNC3CH/A'; //iPhone12，电脑端AppleStore选完型号颜色容量后，浏览器地址栏就会出现型号
let storeId = 'R471'; //西湖Apple Store，填一家离你近的，如果该城市有多家店，其他店有货也会通知你，这里填的优先，对应字段storeNumber
let city = '杭州'; //杭州，直营店的城市，如果该城市有多家店，任何一家有货都会通知你，对应字段city
let barkId = 'nNoL8x77SQVYN8mzQLvVxa'; //Bark APP你的设备号，改成你自己的，否则收不到通知，并且通知接口也会报错
let noticeText = '有货了，抓紧下单'; //通知文案
let isContinue = false; //刷到有货后是否继续刷，false为停止，true为继续刷，停止后需要手动再运行脚本

async function getData() {
  let res = await httpGet(`https://www.apple.com.cn/shop/fulfillment-messages?pl=true&mt=compact&parts.0=${model}&searchNearby=true&store=${storeId}`);
  let stores = res.body.content.pickupMessage.stores.filter((v) => v.city == city);
  stores.forEach((v) => {
    if (v.partsAvailability[model].pickupDisplay == 'available') {
      httpGet(`https://api.day.app/${barkId}/${encodeURI('Apple ' + v.storeName + noticeText)}`);
      if (!isContinue) {
        clearInterval(timer);
      }
    }
    console.log(v.partsAvailability[model].pickupDisplay == 'available' ? '有货了，自动发送通知' : '无货，持续挂机中');
  });
}

timer = setInterval(() => {
  getData();
}, 1000);
